﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using CRM_SMS_MARKETING.DAL;
using CRM_SMS_MARKETING.EL;
using GSM.BusinessLayer;
using GSM.BusinessLayer.DataLayer;

namespace CRM_SMS_MARKETING.PL.Customer
{
    public partial class frmCustomer : DevExpress.XtraEditors.XtraForm
    {
        #region properties
        string User;
        string UserType;
        List<GSM.BusinessLayer.Customer> Listcustomer;
        GSM.BusinessLayer.DataLayer.CustomerSql customersql;
        UserAccountSql UserAccountSql;
        bool Editing;
        bool Adding;
        #endregion
        public frmCustomer(string _User, string _UserType)
        {
            User = _User;
            UserType = _UserType;
            InitializeComponent();
            UserAccountSql = new GSM.BusinessLayer.DataLayer.UserAccountSql();
            //Create lookup edit for filter User
            lookUpUser.Properties.DataSource = UserAccountSql.SelectAllUserName();
            lookUpUser.Properties.DisplayMember = "UserName";
            lookUpUser.Properties.ValueMember = "UserName";
            //Create lookup edit for new Customer
            lookUpEditUser2.Properties.DataSource = UserAccountSql.SelectAllUserName();
            lookUpEditUser2.Properties.DisplayMember = "UserName";
            lookUpEditUser2.Properties.ValueMember = "UserName";
            if (UserType == "Normal")
            {
                lookUpUser.Enabled = false;
            }
            refreshGridCustomer();



        }
        private void btnNew_Click(object sender, EventArgs e)
        {
            Adding = true;
            #region Enabled Button
            btnSave.Enabled = true;
            btnCancel.Enabled = true;
            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
            btnNext.Enabled = false;
            btnPrevious.Enabled = false;
            gridControlCustomer.Enabled = false;
            #endregion
            #region Clear textbox

            txtID.Text = ((int)customersql.GetLastReCordID() + 1).ToString();
            txtFirstName.Text = "";
            txtLastName.Text = "";
            radioGroupGemder.EditValue = true;
            txtAddress.Text = "";
            txtDistrict.Text = "";
            txtCity.Text = "";
            dateEditBirthday.EditValue = DateTime.Now.AddYears(-12);
            txtHomePhone.Text = "";
            txtCellPhone.Text = "";
            txtEmail.Text = "";
            lookUpEditUser2.EditValue = "";

            #endregion
            #region Set ReadOnly
            txtID.Properties.ReadOnly = true;
            txtFirstName.Properties.ReadOnly = false;
            txtLastName.Properties.ReadOnly = false;
            radioGroupGemder.Properties.ReadOnly = false;
            txtAddress.Properties.ReadOnly = false;
            txtDistrict.Properties.ReadOnly = false;
            txtCity.Properties.ReadOnly = false;
            dateEditBirthday.Properties.ReadOnly = false;
            txtHomePhone.Properties.ReadOnly = false;
            txtCellPhone.Properties.ReadOnly = false;
            txtEmail.Properties.ReadOnly = false;
            lookUpEditUser2.Properties.ReadOnly = false;
            #endregion
            #region Enabled Control
            btnNew.Enabled = false;
            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
            btnNext.Enabled = false;
            btnPrevious.Enabled = false;
            btnSave.Enabled = true;
            btnCancel.Enabled = true;

            txtID.Enabled = false;
            txtFirstName.Enabled = true;
            txtLastName.Enabled = true;
            radioGroupGemder.Enabled = true;
            txtAddress.Enabled = true;
            txtDistrict.Enabled = true;
            txtCity.Enabled = true;
            dateEditBirthday.Enabled = true;
            txtHomePhone.Enabled = true;
            txtCellPhone.Enabled = true;
            txtEmail.Enabled = true;
            lookUpEditUser2.Enabled = true;
            #endregion

        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (gridViewCustomer.RowCount != 0)
            {
                Editing = true;
                #region Enabled Control
                btnNew.Enabled = false;
                btnDelete.Enabled = false;
                btnNext.Enabled = false;
                btnPrevious.Enabled = false;
                btnSave.Enabled = true;
                btnCancel.Enabled = true;
                gridControlCustomer.Enabled = false;

                txtID.Enabled = false;
                txtFirstName.Enabled = true;
                txtLastName.Enabled = true;
                radioGroupGemder.Enabled = true;
                txtAddress.Enabled = true;
                txtDistrict.Enabled = true;
                txtCity.Enabled = true;
                dateEditBirthday.Enabled = true;
                txtHomePhone.Enabled = true;
                txtCellPhone.Enabled = true;
                txtEmail.Enabled = true;
                lookUpEditUser2.Enabled = true;
                #endregion
                #region Set ReadOnly
                txtID.Properties.ReadOnly = true;
                txtFirstName.Properties.ReadOnly = false;
                txtLastName.Properties.ReadOnly = false;
                radioGroupGemder.Properties.ReadOnly = false;
                txtAddress.Properties.ReadOnly = false;
                txtDistrict.Properties.ReadOnly = false;
                txtCity.Properties.ReadOnly = false;
                dateEditBirthday.Properties.ReadOnly = false;
                txtHomePhone.Properties.ReadOnly = false;
                txtCellPhone.Properties.ReadOnly = false;
                txtEmail.Properties.ReadOnly = false;
                lookUpEditUser2.Properties.ReadOnly = false;
                #endregion
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (gridViewCustomer.RowCount != 0)
            {
                gridControlCustomer.Enabled = true;
                try
                {
                    CustomerKeys Customerkeys = new CustomerKeys((int)(gridViewCustomer.GetFocusedRowCellValue(colID)));

                    GSM.BusinessLayer.Customer customer = new GSM.BusinessLayer.Customer();
                    customer = customersql.SelectByPrimaryKey(Customerkeys);
                    #region Maping with textbox
                    txtID.Text = customer.CustomerID.ToString();
                    txtFirstName.Text = customer.FirstName.ToString();
                    txtLastName.Text = customer.LastName.ToString();
                    radioGroupGemder.EditValue = (bool)customer.Gender;
                    txtAddress.Text = customer.Addrress.ToString();
                    txtDistrict.Text = customer.District.ToString();
                    txtCity.Text = customer.City.ToString();
                    dateEditBirthday.EditValue = customer.Birthday;
                    txtHomePhone.Text = customer.Phone.ToString();
                    txtCellPhone.Text = customer.CellPhone.ToString();
                    txtEmail.Text = customer.Email.ToString();
                    lookUpEditUser2.EditValue = customer.User.ToString();

                    #endregion
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                #region Enable Control
                txtID.Enabled = true;
                btnCancel.Enabled = false;
                btnSave.Enabled = false;
                btnNew.Enabled = true;
                btnDelete.Enabled = true;
                btnNext.Enabled = true;
                btnPrevious.Enabled = true;
                btnEdit.Enabled = true;
                #endregion
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            bool check = dxValidationProvider1.Validate();
            if (check)
            {
                if (Adding == true)
                {
                    GSM.BusinessLayer.Customer customer = new GSM.BusinessLayer.Customer();

                    #region Mapping field
                    customer.FirstName = txtFirstName.Text;
                    customer.LastName = txtLastName.Text;
                    customer.Gender = (bool)radioGroupGemder.EditValue;
                    customer.Addrress = txtAddress.Text;
                    customer.District = txtDistrict.Text;
                    customer.City = txtCity.Text;
                    customer.Birthday = (DateTime)dateEditBirthday.EditValue;
                    customer.Phone = txtHomePhone.Text = "";
                    customer.CellPhone = txtCellPhone.Text;
                    customer.Email = txtEmail.Text;
                    customer.User = (string)lookUpEditUser2.EditValue;
                    customer.Deleted = false;
                    #endregion
                    bool checkadd = true;
                    try
                    {
                        customersql.Insert(customer);
                    }
                    catch (Exception ex)
                    {
                        checkadd = false;
                        MessageBox.Show(ex.Message);
                    }

                    if (checkadd == true)
                    {
                        MessageBox.Show("Add Customer Successfully!");
                        refreshGridCustomer();

                        //Turn off mode Adding
                        #region  Enabled Control
                        Adding = false;
                        btnSave.Enabled = false;
                        btnCancel.Enabled = false;
                        btnNew.Enabled = true;
                        btnDelete.Enabled = true;
                        btnEdit.Enabled = true;
                        btnNext.Enabled = true;
                        btnPrevious.Enabled = true;
                        gridControlCustomer.Enabled = true;
                        #endregion
                    }
                    else
                    {
                        MessageBox.Show("Add Customer Fail!");
                    }
                }
                if (Editing == true)
                {
                    GSM.BusinessLayer.Customer customer = new GSM.BusinessLayer.Customer();
                    #region Mapping field
                    customer.CustomerID = Int32.Parse(txtID.Text);
                    customer.FirstName = txtFirstName.Text;
                    customer.LastName = txtLastName.Text;
                    customer.Gender = (bool)radioGroupGemder.EditValue;
                    customer.Addrress = txtAddress.Text;
                    customer.District = txtDistrict.Text;
                    customer.City = txtCity.Text;
                    customer.Birthday = (DateTime)dateEditBirthday.EditValue;
                    customer.Phone = txtHomePhone.Text = "";
                    customer.CellPhone = txtCellPhone.Text;
                    customer.Email = txtEmail.Text;
                    customer.User = (string)lookUpEditUser2.EditValue;
                    #endregion
                    bool checkdit = true;
                    try
                    {
                        customersql.Update(customer);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        checkdit = false;
                    }

                    if (checkdit == true)
                    {
                        MessageBox.Show("Edit Customer Successfully!");
                        //Turn off mode Editing
                        #region  Enabled Control
                        Editing = false;
                        btnSave.Enabled = false;
                        btnCancel.Enabled = false;
                        btnNew.Enabled = true;
                        btnDelete.Enabled = true;
                        btnEdit.Enabled = true;
                        btnNext.Enabled = true;
                        btnPrevious.Enabled = true;
                        gridControlCustomer.Enabled = true;
                        #endregion
                        int i = gridViewCustomer.GetFocusedDataSourceRowIndex();
                        refreshGridCustomer();
                        gridViewCustomer.MoveBy(i);
                    }
                    else
                    {
                        MessageBox.Show("Edit Customer Fail!");
                    }


                }
            }
            else
            {
                MessageBox.Show("Some field are not allowed null or blank value");
            }


        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (gridViewCustomer.RowCount != 0)
            {
                if (MessageBox.Show("Are you sure to delete?", "Confirm Delete", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
                {
                    bool checkdel = true;
                    try
                    {

                        GSM.BusinessLayer.Customer customer = new GSM.BusinessLayer.Customer();
                        #region Mapping field
                        customer.CustomerID = Int32.Parse(txtID.Text);
                        customer.FirstName = txtFirstName.Text;
                        customer.LastName = txtLastName.Text;
                        customer.Gender = (bool)radioGroupGemder.EditValue;
                        customer.Addrress = txtAddress.Text;
                        customer.District = txtDistrict.Text;
                        customer.City = txtCity.Text;
                        customer.Birthday = (DateTime)dateEditBirthday.EditValue;
                        customer.Phone = txtHomePhone.Text = "";
                        customer.CellPhone = txtCellPhone.Text;
                        customer.Email = txtEmail.Text;
                        customer.User = (string)lookUpEditUser2.EditValue;
                        customer.Deleted = true;
                        #endregion
                        customersql.Update(customer);
                    }
                    catch (Exception ex)
                    {
                        checkdel = false;
                        MessageBox.Show(ex.Message);
                    }
                    if (checkdel == true)
                    {
                        MessageBox.Show("Delete Customer Successfully!");
                        gridViewCustomer.MovePrev();
                        refreshGridCustomer();
                    }
                    else
                    {
                        MessageBox.Show("Delete Customer Fail!");
                    }

                }
            }

        }

        int CheckLast = 1;
        private void btnNext_Click(object sender, EventArgs e)
        {
            gridViewCustomer.MoveNext();
            if (gridViewCustomer.IsLastRow)
            {
                gridViewCustomer.MoveLast();
                CheckLast++;
                if (CheckLast % 2 == 1)
                {
                    gridViewCustomer.MoveFirst();
                }
            }

        }

        int checkFirst = 1;
        private void btnPrevious_Click(object sender, EventArgs e)
        {
            gridViewCustomer.MovePrev();
            if (gridViewCustomer.IsFirstRow)
            {
                gridViewCustomer.MoveFirst();
                checkFirst++;
                if (checkFirst % 2 == 1)
                {
                    gridViewCustomer.MoveLast();
                }
            }
        }
        private void refreshGridCustomer()
        {
            if (UserType == "Admin")
            {
                customersql = new CustomerSql();
                Listcustomer = customersql.SelectByField("Deleted", 0);
                gridControlCustomer.DataSource = Listcustomer;
            }
            else
            {
                customersql = new CustomerSql();
                Listcustomer = customersql.SelectByField("User", User);
                gridControlCustomer.DataSource = Listcustomer;
            }

            try
            {
                CustomerKeys CustomerKey = new CustomerKeys((int)(gridViewCustomer.GetFocusedRowCellValue(colID)));
                GSM.BusinessLayer.Customer customer = new GSM.BusinessLayer.Customer();
                customer = customersql.SelectByPrimaryKey(CustomerKey);
                MappingField(customer);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void gridControlCustomer_Load(object sender, EventArgs e)
        {
            //refreshGridCustomer();


        }

        private void gridViewCustomer_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                CustomerKeys CustomerKey = new CustomerKeys((int)(gridViewCustomer.GetRowCellValue(e.RowHandle, colID)));
                GSM.BusinessLayer.Customer customer = new GSM.BusinessLayer.Customer();
                customer = customersql.SelectByPrimaryKey(CustomerKey);
                MappingField(customer);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gridViewCustomer_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            gridViewCustomer_RowClick(sender, e);
        }

        private void MappingField(GSM.BusinessLayer.Customer customer)
        {
            #region Maping with textbox
            txtID.Text = customer.CustomerID.ToString();
            txtFirstName.Text = customer.FirstName.ToString();
            txtLastName.Text = customer.LastName.ToString();
            radioGroupGemder.EditValue = (bool)customer.Gender;
            txtAddress.Text = customer.Addrress.ToString();
            txtDistrict.Text = customer.District.ToString();
            txtCity.Text = customer.City.ToString();
            dateEditBirthday.EditValue = customer.Birthday;
            txtHomePhone.Text = customer.Phone.ToString();
            txtCellPhone.Text = customer.CellPhone.ToString();
            txtEmail.Text = customer.Email.ToString();
            lookUpEditUser2.EditValue = customer.User.ToString();
            #endregion
        }

        private void gridViewCustomer_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (Listcustomer.Count == 0)
            {
                #region Clear textbox

                txtID.Text = "";
                txtFirstName.Text = "";
                txtLastName.Text = "";
                radioGroupGemder.EditValue = null;
                txtAddress.Text = "";
                txtDistrict.Text = "";
                txtCity.Text = "";
                dateEditBirthday.EditValue = null;
                txtHomePhone.Text = "";
                txtCellPhone.Text = "";
                txtEmail.Text = "";
                lookUpEditUser2.EditValue = "";

                #endregion
            }
            else
            {
                try
                {
                    CustomerKeys CustomerKey = new CustomerKeys((int)(gridViewCustomer.GetFocusedRowCellValue(colID)));
                    GSM.BusinessLayer.Customer customer = new GSM.BusinessLayer.Customer();
                    customer = customersql.SelectByPrimaryKey(CustomerKey);
                    MappingField(customer);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

        }

        private void frmCustomer_Load(object sender, EventArgs e)
        {
            labelUser.Text = User;

        }

        private void lookUpUser_EditValueChanged(object sender, EventArgs e)
        {
            if (lookUpUser.EditValue.ToString() == "All")
            {
                gridControlCustomer.DataSource = customersql.SelectByField("Deleted", 0);
            }
            else
            {

                Listcustomer = customersql.SelectByField("User", lookUpUser.EditValue.ToString());
                if (Listcustomer.Count == 0)
                {
                    List<GSM.BusinessLayer.Customer> list = new List<GSM.BusinessLayer.Customer>();
                    gridControlCustomer.DataSource = list;
                }
                else
                {
                    gridControlCustomer.DataSource = Listcustomer;
                    if (gridViewCustomer.RowCount != 0)
                    {
                        try
                        {
                            CustomerKeys CustomerKey = new CustomerKeys((int)(gridViewCustomer.GetFocusedRowCellValue(colID)));
                            GSM.BusinessLayer.Customer customer = new GSM.BusinessLayer.Customer();
                            customer = customersql.SelectByPrimaryKey(CustomerKey);
                            MappingField(customer);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                }

            }
        }
    }
}